<?php

namespace model;

use think\Model;
class BaseModel extends Model
{
    // 开启自动写入时间戳字段
//    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
//    protected $createTime = 'addtime';
    protected $type = [
//        'create_time'  =>  'timestamp:Y-m-d H:i:s',
        // 'start_time'  =>  'timestamp:Y-m-d H:i:s',
        // 'end_time'  =>  'timestamp:Y-m-d H:i:s',
    ];
    protected static $all_limit=100;
    //插入单挑数据
    public static function insertInfo($data)
    {
        return self::insertGetId($data);
    }
    //插入多条数据
    public static function insertMore($data)
    {
        return self::limit(self::$all_limit)->insertAll($data);
    }
    //条件更新
    public static function updateInfo($where,$data)
    {
        $res= self::where($where)->update($data);
        if($res!==false){
            return true;
        }else{
            return  false;
        }
    }
    //获取数据条数
    public static function getCount($where)
    {
        return self::where($where)->count();
    }
    //获取单条数据
    public static function getInfo($where, $field = '*')
    {
        $q = self::field($field)->where($where)->find();
        return $q?$q->toArray():$q;
    }
    //获取多条数据
    public static function getList($where='',$field='*',$order=null)
    {
        $query  = self::where($where)->field($field);
        if($order){
            $query->order($order);
        }
        return $query->select()->toArray();
    }
    //或条件获取多条数据
    public static function getListWhereOr($whereOr='',$field='*',$order=null)
    {
        $query  = self::whereOr($whereOr)->field($field);
        if($order){
            $query->order($order);
        }
        return $query->select()->toArray();
    }
    //获取key-value 多条数据
    public static function getListColumn($where,$field='*',$key='id')
    {
        return self::where($where)->column($field,$key);
    }
    //获取单条数据的某个元素
    public static function getValue($where,$field,$order='id asc')
    {
        return self::where($where)->order($order)->value($field);
    }
    //条件获取 某字段sum
    public static function getSum($where,$field)
    {
        return self::where($where)->sum($field);
    }
    //主键删除 单条/多条
    public static function destroyInfo($id)
    {
        return self::destroy($id);
    }
    //条件删除
    public static function delInfo($where){
        $res= self::where($where)->delete();
        if($res!==false){
            return true;
        }else{
            return  false;
        }
    }
}
